package com.xinglai.yun.wxapi;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.action.childbase.ChildBaseActivity;
import com.action.entity.event.WeiXinLoginSuccess;
import com.action.ui.activity.XYBMainActiviy;
import com.action.ui.activity.RegisterRuleActivity;
import com.action.utils.LocUtils;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.base.app_core.App;
import com.base.app_core.model.Paginated;
import com.base.app_core.service.pressenter.XYBUPressenter;
import com.base.app_core.service.user.model.User;
import com.base.app_core.util.AppConstants;
import com.base.app_core.util.BeanPropertiesUtil;
import com.base.app_core.util.Preferences;
import com.base.app_core.util.XYBAUtils;
import com.base.app_core.util.log.F;
import com.hwangjr.rxbus.RxBus;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 *
 */
@Route(path = AppConstants.ROUTEURI.LOGIN)
public class WXEntryActivity extends ChildBaseActivity<XYBUPressenter> implements IWXAPIEventHandler {

    private boolean isFromLogin;

    @Override
    protected int getLayoutId() {
        return com.xinglai.yun.R.layout.fragment_entry;
    }

    @Inject
    User user;

    @BindView(com.xinglai.yun.R.id.btn_entry)
    Button btnEntry;

    @Inject
    LocUtils locUtils;

    @Override
    protected void setupComponent() {
        super.setupComponent();
        getChildViewComponent().inject(this);
    }

    private void requestWxLogin() {
        if (XYBAUtils.checkApkExist(this)) {
            SendAuth.Req req = new SendAuth.Req();
            req.scope = "snsapi_userinfo";
            req.state = "wechat_sdk_demo_test";
            showProgress();
            boolean result = App.getApp().iwxapi.sendReq(req);
            F.j(result + "");
        } else {
            showToast("请安装微信");
        }
    }

    @OnClick({com.xinglai.yun.R.id.btn_entry, com.xinglai.yun.R.id.ll_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case com.xinglai.yun.R.id.btn_entry:
                Preferences.putBoolean(AppConstants.SP.IS_FIRST, false);
                requestWxLogin();
                break;
            case com.xinglai.yun.R.id.ll_register:
                startActivity(new Intent(this, RegisterRuleActivity.class));
                break;
        }
    }

    @Override
    protected void initView() {
        super.initView();
        setToolBar("登录");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //注册API
        App.getApp().iwxapi.handleIntent(getIntent(), this);
        isFromLogin = getIntent().getBooleanExtra(AppConstants.EXTRA.IS_FROM_LOGIN, false);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        if (!(baseResp instanceof SendAuth.Resp)) {
            finish();
            return;
        }
        int errCode = baseResp.errCode;
        if (errCode == 0) {
            if (baseResp instanceof SendAuth.Resp) {
                String code = ((SendAuth.Resp) baseResp).code; //即为所需的code
                F.j("content:" + code);
                RxBus.get().post(new WeiXinLoginSuccess());
                mPresenter.wxLogin(code);
                showProgress();
            } else {
                this.finish();
            }
        } else {
            dismissProgress();
            if (errCode == -2) {
                showToast("请重新授权");
            }
        }

    }

    @Override
    public <T> void notifyData(T object, Class tClass, Paginated paginated) {
//        super.notifyData(object, tClass, paginated);
//        dismissProgress();
        if (object instanceof User) {
            showToast("登录成功");
            try {
                BeanPropertiesUtil.copyProperties(object, user);
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (user.getLat() != null && user.getLng() != null) {
                Preferences.putString(AppConstants.SP.LAT, String.valueOf(user.getLat()));
                Preferences.putString(AppConstants.SP.LNG, String.valueOf(user.getLng()));
                Preferences.putLong(AppConstants.SP.RANGE, user.getRange());
                Preferences.putString(AppConstants.SP.E, user.getE());
                Preferences.putString(AppConstants.SP.N, user.getN());
            }
            startActivity(new Intent(this, XYBMainActiviy.class));
            this.finish();
        }
    }
}
